package com.monkeyk.os.service.dto;

import com.monkeyk.os.domain.oauth.ClientDetail;
import com.monkeyk.os.domain.shared.GuidGenerator;
import com.monkeyk.os.domain.users.Roles;
import org.apache.commons.lang.StringUtils;
import org.apache.oltu.oauth2.common.message.types.GrantType;

import java.util.ArrayList;
import java.util.List;

/**
 * 2016/6/8
 *
 * @author Shengzhao Li
 */
public class ClientDetailFormDto extends ClientDetailDto {

    private static final long serialVersionUID = -5827571696766683709L;

    private List<RoleDto> rolesDtoList = new ArrayList<>();

    public ClientDetailFormDto() {
        //set  default value
        this.setClientId(GuidGenerator.generateClientId());
        this.setClientSecret(GuidGenerator.generateClientSecret());
    }

    public ClientDetailFormDto(ClientDetail details) {
        super(details);
    }

    public ClientDetailFormDto(List<Roles> rolesList) {
        this();
        this.rolesDtoList = RoleDto.toDtos(rolesList);
    }

    public GrantType[] getAvailableGrantTypes() {
        return GrantType.values();
    }

    public List<RoleDto> getRolesDtoList() {
        return rolesDtoList;
    }

    public void setRolesDtoList(List<RoleDto> rolesDtoList) {
        this.rolesDtoList = rolesDtoList;
    }

    public ClientDetail newClientDetails() {

        final ClientDetail clientDetail = new ClientDetail();
        clientDetail.setClientId(getClientId());
        clientDetail.setClientSecret(getClientSecret());
        clientDetail.setClientUri(getClientUri());
        clientDetail.setName(getClientName());
        clientDetail.setDescription(getDescription());
        clientDetail.setIconUri(getClientIconUri());
        clientDetail.setRedirectUri(getRedirectUri());
        clientDetail.resourceIds(getResourceIds());
        clientDetail.scope(getScope());
        clientDetail.grantTypes(getGrantTypes());
        final String roles = getRoles();
        clientDetail.roles(StringUtils.isEmpty(roles) ? null : roles);
        clientDetail.accessTokenValidity(getAccessTokenValidity());
        clientDetail.refreshTokenValidity(getRefreshTokenValidity());
        clientDetail.trusted(isTrusted());
        return clientDetail;
    }
}
